import { ExamTopicListDateType } from '@/util/interface'
import { DataType } from './interface'
import type { PayloadAction } from '@reduxjs/toolkit'

// 修改阅卷列表的数据
export const setExamListData = (state: DataType, action: PayloadAction<any>) => {
  state.exam_list_data = {
    ...state.exam_list_data,
    ...action.payload
  }
}

// 设置树型选择中二级课程的高亮
export const setSubjectTwoActive = (state: DataType, action: PayloadAction<any>) => {
  state.subject_two_active = action.payload
}

// 被选中的题目
export const setActiveTopic = (state: DataType, action: PayloadAction<any>) => {
  state.active_topic = action.payload
}

// 修改题目ID
export const setCurrentExamTopicId = (state: DataType, action: PayloadAction<any>) => {
  state.current_exam_topic_id = action.payload
}

// 阅卷时候的批阅添加的批阅，通过等信息
export const setCorretExamTopicList = (state: DataType, action: PayloadAction<any>) => {
  state.exam_topic_list.forEach((item: any) => {
    if (item._id === action.payload._id) {
      item.pass = action.payload.pass
      item.comment = action.payload.comment
      item.is_corret = action.payload.is_corret
    }
  })
}

// 设置当前被选中的二级课程
export const setCurrentSubjectTwo = (state: DataType, action: PayloadAction<any>) => {
  state.current_subject_two = action.payload
}

// 保存答题时候的答案-------进行持久化存储
export const setAnswerExamTopicList = (state: DataType, action: PayloadAction<any>) => {
  state.exam_topic_list.forEach((item: ExamTopicListDateType) => {
    if (item._id === action.payload._id) {
      item.answer = action.payload.answer
    }
  })
  let local_answer = JSON.parse(localStorage.getItem('topic')!)
  // 如果本地已经存储了topic
  if (local_answer) {
    local_answer.forEach((item: ExamTopicListDateType) => {
      if (item._id === action.payload._id) {
        item.answer = action.payload.answer
      }
    })

    localStorage.setItem('topic', JSON.stringify(local_answer))
  } else {
    localStorage.setItem('topic', JSON.stringify(state.exam_topic_list))
  }
}

// 清空考试题目
export const setClearExamTopicList = (state: DataType, action: PayloadAction<any>) => {
  state.exam_topic_list = action.payload
}
